var gulp = require('gulp');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var cssmin = require('gulp-clean-css'), //css压缩
    concat = require('gulp-concat'), //合并文件
    uglify = require('gulp-uglify'), //js压缩
    rename = require('gulp-rename'), //文件重命名
    notify = require('gulp-notify'); //提示
    postcss = require('gulp-postcss'); //自动补全css
    imagemin = require('gulp-imagemin');//图片压缩
var autoprefixer = require('autoprefixer');
var sass = require('gulp-sass');


//css处理
gulp.task('minifycss', function() {
    return gulp.src('app/css/*.scss') //设置css
        .pipe(sass({outputStyle: 'compressed'}))
        .on('error', sass.logError) // 错误信息
        // .pipe(postcss([ autoprefixer() ]))
        .pipe(cssmin())
        .pipe(concat('style.css')) //合并css文件到"style"
        .pipe(gulp.dest('dist/css')) //输出文件目录
        .pipe(notify({ message: 'css task ok' })); //提示成功
});

//JS处理
/* gulp.task('minifyjs', function() {
    return gulp.src([
                'app/js/*.js',
         ]) //选择合并的JS
        .pipe(concat('js.js')) //合并js
        .pipe(uglify({
            // mangle: {except: ['require' ,'exports' ,'module' ,'$']},//排除混淆关键字,//类型：Boolean 默认：true 是否修改变量名
            // mangle: true,//类型：Boolean 默认：true 是否修改变量名
            compress: true,//类型：Boolean 默认：true 是否完全压缩
            ie8:false,//是否支持ie8
            // preserveComments: 'all' //保留所有注释
        })) //压缩
        .pipe(gulp.dest('dist/js')) //输出
        .pipe(notify({ message: "js task ok" })); //提示
}); */


//拷贝图片
/* gulp.task('moveimg', function() {
    return gulp.src('app/images/*.*')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images')); //提示
});
gulp.task('movepic', function() {
    return gulp.src('app/picture/*.*')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/picture')); //提示
}); */


//拷贝HTML
gulp.task('movehtml',function(){
   return gulp.src('app/*.html')
       .pipe(gulp.dest('dist/'));    //提示
});


// 监视文件改动并重新载入
gulp.task('serve', ['minifycss','movehtml'], function() {
    browserSync({
        server: {
          baseDir: 'dist/'
        }
        // proxy: "http://fk.dev/"
    });

    // gulp.watch(['js/*.js'], { cwd: './app' }, ['minifyjs']).on('change', reload);
    // gulp.watch(['images/*.*'], { cwd: './app' }, ['moveimg']).on('change', reload);
    // gulp.watch(['picture/*.*'], { cwd: './app' }, ['movepic']).on('change', reload);
    gulp.watch(['css/*.scss'], {
        cwd: './app'
    }, ['minifycss']).on('change', reload);
    gulp.watch(['*.html'], { cwd: './app' }, ['movehtml']).on('change', reload);
});
